package bg.cinema_system.beans;

import java.sql.SQLException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;

import bg.cinema_system.service.UserServeice;

@ManagedBean(name = "chagePassword")
@RequestScoped
public class ChangePasswordBean {

	private String oldPassword;
	private String newPassword;
	private String repeatPassword;
	private String warning;
	private String message;
	private boolean checkOldPassword;

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getRepeatPassword() {
		return repeatPassword;
	}

	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}

	public String getWarning() {
		return warning;
	}

	public void setWarning(String warning) {
		this.warning = warning;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isCheckOldPassword() {
		return checkOldPassword;
	}

	public void setCheckOldPassword(boolean checkOldPassword) {
		this.checkOldPassword = checkOldPassword;
	}

	public void passwordValidator(FacesContext context, UIComponent toValidate,
			Object value) {
		UIInput newPasswordField = (UIInput) context.getViewRoot()
				.findComponent("changepass-form:newPassword");

		if (newPasswordField == null) {
			throw new IllegalArgumentException(
					String.format("Unable to find component."));
		}

		String newPass = (String) newPasswordField.getValue();
		String repeatPass = (String) value;

		if (!(repeatPass != null && repeatPass.length() >= 5)) {
			throwException("*Length is less than allowable minimum of '5'!");
		}

		if (!repeatPass.equals(newPass)) {
			throwException("*Passwords do not match!");
		}

		UIInput oldPasswordField = (UIInput) context.getViewRoot()
				.findComponent("changepass-form:oldPassword");

		if (oldPasswordField == null) {
			throw new IllegalArgumentException(
					String.format("Unable to find component."));
		}

		String oldPass = (String) oldPasswordField.getValue();

		if (oldPass != null && newPass != null && oldPass.equals(newPass)
				&& newPass.equals(repeatPass) && checkOldPassword) {
			throwException("*Please use different password!");
		}
	}

	public void oldPasswordValidator(FacesContext context,
			UIComponent toValidate, Object value) {

		String oldPass = (String) value;
		this.checkOldPassword = false;

		if (!(oldPass != null && oldPass.length() >= 5)) {
			throwException("*Length is less than allowable minimum of '5'!");
		}

		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(false);
		int userId = (Integer) session.getAttribute("userId");

		try {
			if (!UserServeice.checkPassword(userId, oldPass)) {
				throwException("*Your current password is wrong!");
			}
		} catch (SQLException e) {
			throwException("*Your current password is wrong!");
		}

		this.checkOldPassword = true;
	}

	public String changePassword() {
		try {
			HttpSession session = (HttpSession) FacesContext
					.getCurrentInstance().getExternalContext()
					.getSession(false);
			int userId = (Integer) session.getAttribute("userId");

			UserServeice.changePassword(userId, getRepeatPassword());
		} catch (SQLException e) {
			e.printStackTrace();
			setWarning("Password does not changed. Try again!");
		}

		setMessage("Successfully changed password!");
		return "changePass";
	}

	private void throwException(String messageText) {
		FacesMessage message = new FacesMessage(messageText);
		throw new ValidatorException(message);
	}

}
